package org.hackystat.sdsa.analyzer.control.processor;

import java.util.logging.Logger;

import javax.xml.datatype.XMLGregorianCalendar;

import org.hackystat.sdsa.analyzer.model.activity.Activity;
import org.hackystat.sdsa.analyzer.model.activity.UnitTestActivity;
import org.hackystat.sensorbase.resource.sensordata.jaxb.Properties;
import org.hackystat.sensorbase.resource.sensordata.jaxb.Property;
import org.hackystat.sensorbase.resource.sensordata.jaxb.SensorData;
import org.hackystat.utilities.tstamp.Tstamp;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

/**
 * Test unit test activity processor.
 * 
 * 
 * @author hongbing
 *
 */
public class TestUnitTestActivityProcessor {
  /** UnitTestActivityProcessor */
  private UnitTestActivityProcessor processor;
  private Logger logger;
  
  /**
   * Prepares test variables.
   * 
   * @throws Exception If error in testing. 
   */
  @Before public void setUp() throws Exception {
    this.processor = new UnitTestActivityProcessor();
    this.logger = Logger.getLogger(TestUnitTestActivityProcessor.class.getName());
  }
  
  /**
   * Test unit test success processor.
   * 
   * @throws Exception If error in testing.
   */
  @Test public void testUnitTestSuccess() throws Exception {
    /** UnitTest data. */
    SensorData unitTestData = new SensorData();
    XMLGregorianCalendar tstamp = Tstamp.makeTimestamp("2007-11-17T10:25:16.949-08:00");
    unitTestData.setTimestamp(tstamp);
    unitTestData.setResource("file:/C:/cvs/work/com/foo/TestBar.java");
    unitTestData.setSensorDataType("DevEvent");
    XMLGregorianCalendar runtimeCal = Tstamp.makeTimestamp("2007-11-17T10:25:00.000-08:00");
    unitTestData.setRuntime(runtimeCal);
    
    // Add property
    Properties props = new Properties();
    unitTestData.setProperties(props);

    Property type = new Property();
    type.setKey("Type");
    type.setValue("Test");
    props.getProperty().add(type);
    
    Property failureString = new Property();
    failureString.setKey(UnitTestActivity.FAILURE_STRING);
    failureString.setValue("");
    props.getProperty().add(failureString);

    Property elapsedTime = new Property();
    elapsedTime.setKey(UnitTestActivity.ELAPSED_TIME);
    elapsedTime.setValue("12");
    props.getProperty().add(elapsedTime);

    Property testName = new Property();
    testName.setKey(UnitTestActivity.TEST_NAME);
    testName.setValue("testSingle()");
    props.getProperty().add(testName);

    Property testCase = new Property();
    testCase.setKey(UnitTestActivity.TEST_CASE);
    testCase.setValue("TestBar");
    props.getProperty().add(testCase);

    Property testCount = new Property();
    testCount.setKey(UnitTestActivity.TEST_COUNT);
    testCount.setValue("1");
    props.getProperty().add(testCount);

    Property testIndice = new Property();
    testIndice.setKey(UnitTestActivity.TEST_INDICE);
    testIndice.setValue("1");
    props.getProperty().add(testIndice);

    
    EditActivityProcessor editProcessor = new EditActivityProcessor();
    Assert.assertFalse("Test processable of document edit activity", 
        editProcessor.processable(unitTestData));

    Assert.assertTrue("Test processable of unit test activity", 
           this.processor.processable(unitTestData));
    
    Activity activity = this.processor.process(unitTestData, this.logger);
    Assert.assertTrue("Test return type", activity instanceof UnitTestActivity);

    UnitTestActivity unitTestActivity = (UnitTestActivity) activity;
    Assert.assertEquals("Test failure message", "", unitTestActivity.getFailureMessage());
    
    Assert.assertEquals("Test test count", 1, unitTestActivity.getTestCount());
    Assert.assertEquals("Test test indice", 1, unitTestActivity.getTestIndice());
  }
    
}
